//
//  MainViewController.m
//  Normal
//
//  Created by 8b410-2 on 16/12/9.
//  Copyright © 2016年 abb. All rights reserved.
//

#import "OSCMainViewController.h"
#import "OSCMainViewModel.h"
#import "OSCHomeViewController.h"
#import "OSCBasePageViewController.h"
#import "OSCBaseNavigationController.h"
#import "OSCTweetTableViewController.h"
#import "AppDelegate.h"
#import <AFNetworking/AFNetworkReachabilityManager.h>
@interface OSCMainViewController ()
@property (nonatomic, strong) OSCMainViewModel *viewModel;
@property (strong,nonatomic)UIButton *composeButton;
@property (assign,nonatomic)AFNetworkReachabilityStatus reachabilityStatus;
@end

@implementation OSCMainViewController

@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addChildViewControllers];
}

- (void)setupComposeButton {
    [self.tabBarController.tabBar addSubview:self.composeButton];
    
    CGPoint origin = [self.view convertPoint:self.tabBarController.tabBar.center toView:self.tabBarController.tabBar];
    CGSize buttonSize = CGSizeMake(self.tabBarController.tabBar.frame.size.width / 5 - 6, self.tabBarController.tabBar.frame.size.height - 4);
    
    self.composeButton.frame = CGRectMake(origin.x - buttonSize.height/2, origin.y - buttonSize.height/2, buttonSize.height, buttonSize.height);
    
}
- (void)addChildViewControllers {
    OSCBaseNavigationController *navNewsVC = [self addNavigationForViewController:[[OSCHomeViewController alloc] init]];
    OSCBaseNavigationController *navTweetsVC = [self addNavigationForViewController:[[OSCBasePageViewController alloc] initWithViewModel:self.viewModel.tweetPageViewModel]];
    self.tabBarController.viewControllers = @[navTweetsVC,
                                              navNewsVC,
                                              [UIViewController new],
                                              [UIViewController new],
                                              [UIViewController new]
                                              ];
    
    NSArray *titles = @[@"动弹", @"综合", @"", @"发现", @"我的"];
    NSArray *images = @[@"tabbar-tweet", @"tabbar-news", @"", @"tabbar-discover", @"tabbar-me"];
    [self.tabBarController.tabBar.items enumerateObjectsUsingBlock:^(UITabBarItem *item, NSUInteger idx, BOOL *stop) {
        [item setTitle:titles[idx]];
        item.image = [[UIImage imageNamed:images[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item.selectedImage = [[UIImage imageNamed:[images[idx] stringByAppendingString:@"-selected"]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }];
    [self.tabBarController.tabBar.items[2] setEnabled:NO];
    self.tabBarController.tabBar.translucent = NO;
    [self setupComposeButton];
    [OSCSharedAppDelegate.navigationControllerStack pushNavigationController:navTweetsVC];
    [[[self
      rac_signalForSelector:@selector(tabBarController:didSelectViewController:)
      fromProtocol:@protocol(UITabBarControllerDelegate)]
      filter:^BOOL(RACTuple *tuple) {
          if ([tuple.second isKindOfClass:[UINavigationController class]]) {
              return YES;
          }
          return NO;
      }]
     subscribeNext:^(RACTuple *tuple) {
         [OSCSharedAppDelegate.navigationControllerStack popNavigationController];
         [OSCSharedAppDelegate.navigationControllerStack pushNavigationController:tuple.second];
     }];
    self.tabBarController.delegate = self;
}
#pragma mark - 添加nav到vc上
- (OSCBaseNavigationController *)addNavigationForViewController:(UIViewController *)viewController {
    OSCBaseNavigationController *navigationController = [[OSCBaseNavigationController alloc] initWithRootViewController:viewController];
    return navigationController;
}

- (UIButton *)composeButton {
    if (!_composeButton) {
        _composeButton = [UIButton buttonWithImageNameStr:@"ic_nav_add" BackgroundColor:nil];
        [_composeButton setImage:[UIImage imageNamed:@"ic_nav_add_actived"] forState:UIControlStateHighlighted];
        [_composeButton addTarget:self action:@selector(clickCompose) forControlEvents:UIControlEventTouchUpInside];
    }
    return _composeButton;
}
- (void)clickCompose {
    [self.viewModel toTweetComposeViewModel];
}

@end
